-- Themes.lua — Damage & Healer color themes + apply helpers
local ADDON, DFFT = ...
if not DFFT or not DFFT.DB then return end
local DB = DFFT.DB

-- ---------- utils ----------
local function hex(h)
  h = (h or ""):gsub("#","")
  local r = tonumber(h:sub(1,2),16) or 255
  local g = tonumber(h:sub(3,4),16) or 255
  local b = tonumber(h:sub(5,6),16) or 255
  return {r/255, g/255, b/255}
end
local function copyRGB(c) if not c then return {1,1,1} end return {c[1] or 1, c[2] or 1, c[3] or 1} end

-- ==========================================================
-- THEMES DEGATS
-- Chaque thème définit :
--  smallColor, mediumColor, bigColor, veryBigColor, nonTargetColor
-- ==========================================================
DFFT.DamageThemes = {
  ["Pastel"] = {
    smallColor     = hex("#c5c5c5"),
    mediumColor    = hex("#f4e8cb"),
    bigColor       = hex("#f3cbda"),
    veryBigColor   = hex("#ecb9b8"),
    nonTargetColor = hex("#aedcd8"),
  },

  -- "Blizzard UI" devient "Basic" (alias plus bas)
  ["Basic"] = {
    smallColor     = {0.70, 0.70, 0.70},
    mediumColor    = {1.00, 1.00, 1.00},
    bigColor       = {1.00, 0.85, 0.00},
    veryBigColor   = {1.00, 0.55, 0.00},
    nonTargetColor = {0.45, 0.75, 1.00},
  },

  -- Nouveau thème "Warcraft" : nuances de jaune autour de #f8de00
  -- (nonTarget volontairement bleu, pas jaune)
  ["Warcraft"] = {
    smallColor     = hex("#FFF3A6"), -- doux
    mediumColor    = hex("#F8DE00"), -- jaune emblématique
    bigColor       = hex("#FFC700"),
    veryBigColor   = hex("#FFAB00"),
    nonTargetColor = hex("#2A8FFF"), -- bleu lisible
  },

  ["Sanguin"] = {
    smallColor     = hex("#8b4a4a"),
    mediumColor    = hex("#b04343"),
    bigColor       = hex("#cf2626"),
    veryBigColor   = hex("#8f0000"),
    nonTargetColor = hex("#5aa2cf"),
  },
  ["Arcade Néon"] = {
    smallColor     = hex("#00ffb7"),
    mediumColor    = hex("#00ffff"),
    bigColor       = hex("#ff00ff"),
    veryBigColor   = hex("#ffff00"),
    nonTargetColor = hex("#1ec8ff"),
  },
  ["Glacial"] = {
    smallColor     = hex("#9fd5ff"),
    mediumColor    = hex("#65b5ff"),
    bigColor       = hex("#2f7fff"),
    veryBigColor   = hex("#124fd8"),
    nonTargetColor = hex("#85e6e6"),
  },
  ["Embers"] = {
    smallColor     = hex("#e0c9a9"),
    mediumColor    = hex("#f1b06a"),
    bigColor       = hex("#ff7b2b"),
    veryBigColor   = hex("#ff3a0a"),
    nonTargetColor = hex("#73c3ff"),
  },
  ["Toxic"] = {
    smallColor     = hex("#c7ff8a"),
    mediumColor    = hex("#9fff3d"),
    bigColor       = hex("#66d815"),
    veryBigColor   = hex("#33b104"),
    nonTargetColor = hex("#7de0d8"),
  },

  -- Renommé : Monochrome+Or -> Golden
  ["Golden"] = {
    smallColor     = hex("#aeaeae"),
    mediumColor    = hex("#dedede"),
    bigColor       = hex("#ffd36b"),
    veryBigColor   = hex("#ffad2e"),
    nonTargetColor = hex("#93c8ff"),
  },
}

-- Alias rétrocompatibles
DFFT.DamageThemes["Blizzard UI"]   = DFFT.DamageThemes["Basic"]   -- anciens profils
DFFT.DamageThemes["Monochrome+Or"] = DFFT.DamageThemes["Golden"]  -- anciens profils

-- Applique les couleurs dégâts dans la DB
function DFFT.ApplyDamageTheme(name)
  if name == "Blizzard UI"   then name = "Basic" end
  if name == "Monochrome+Or" then name = "Golden" end
  local t = DFFT.DamageThemes[name or ""]
  if not t then return end
  DB.smallColor     = copyRGB(t.smallColor)
  DB.mediumColor    = copyRGB(t.mediumColor)
  DB.bigColor       = copyRGB(t.bigColor)
  DB.veryBigColor   = copyRGB(t.veryBigColor)
  DB.nonTargetColor = copyRGB(t.nonTargetColor)
  DB.damageTheme    = name
  if print then print(("|cff88ff88SCT|r 伤害主题已应用: %s"):format(name)) end
end

-- ==========================================================
-- THEMES HEALER (Soins + Absorbs)
-- Chaque thème définit :
--   target    = { small=RGB, medium=RGB, big=RGB, vbig=RGB }
--   nontarget = { small=RGB, medium=RGB, big=RGB, vbig=RGB }
--   absorb    = RGB (couleur unique pour les absorb)
-- ==========================================================
DFFT.HealThemes = {
  ["Mint"] = {
    target    = { small=hex("#ddffaa"), medium=hex("#c1ff63"), big=hex("#adff2f"), vbig=hex("#95d009") },
    nontarget = { small=hex("#a2f6da"), medium=hex("#6eecc2"), big=hex("#33e1b9"), vbig=hex("#00d0ad") },
    absorb    = hex("#FFE4B5"),
  },
  ["Emerald"] = {
    target    = { small=hex("#d8ffd8"), medium=hex("#9aff9a"), big=hex("#41e36b"), vbig=hex("#11c451") },
    nontarget = { small=hex("#b8f3cf"), medium=hex("#83e7bb"), big=hex("#39d49f"), vbig=hex("#0fb98f") },
    absorb    = hex("#FFF0C6"),
  },
  ["Aquatic"] = {
    target    = { small=hex("#c7fff5"), medium=hex("#8af2e6"), big=hex("#38e0d0"), vbig=hex("#14c5b5") },
    nontarget = { small=hex("#cfe6ff"), medium=hex("#9ed0ff"), big=hex("#63b4ff"), vbig=hex("#2a8fff") },
    absorb    = hex("#E7F5FF"),
  },
  ["Golden Remedy"] = {
    target    = { small=hex("#fff3c4"), medium=hex("#ffe08a"), big=hex("#ffd04d"), vbig=hex("#ffb600") },
    nontarget = { small=hex("#d6f0c2"), medium=hex("#aee09f"), big=hex("#6cc67e"), vbig=hex("#3dbb75") },
    absorb    = hex("#FFF2C9"),
  },
  ["Cotton"] = {
    target    = { small=hex("#ffffff"), medium=hex("#f4faf2"), big=hex("#eaf7ea"), vbig=hex("#d6f0d6") },
    nontarget = { small=hex("#eaf7ff"), medium=hex("#d6efff"), big=hex("#c2e7ff"), vbig=hex("#a8dbff") },
    absorb    = hex("#FFF5E0"),
  },
  ["Lavender"] = {
    target    = { small=hex("#efe1ff"), medium=hex("#d8bbff"), big=hex("#b68aff"), vbig=hex("#8f5cff") },
    nontarget = { small=hex("#d0f1ff"), medium=hex("#a9e4ff"), big=hex("#6fd1ff"), vbig=hex("#3bbfff") },
    absorb    = hex("#F3E8FF"),
  },
  ["Cleric"] = {
    target    = { small=hex("#e4ffd8"), medium=hex("#b8ff9a"), big=hex("#80ef55"), vbig=hex("#33d12a") },
    nontarget = { small=hex("#d5ffe8"), medium=hex("#aaf3d4"), big=hex("#6fe4c1"), vbig=hex("#29c6a8") },
    absorb    = hex("#FFF0D9"),
  },
  ["Teal & Amber"] = {
    target    = { small=hex("#d8fff8"), medium=hex("#a6ffee"), big=hex("#5ff5dc"), vbig=hex("#27e3c5") },
    nontarget = { small=hex("#fff1d0"), medium=hex("#ffd586"), big=hex("#ffbf47"), vbig=hex("#ff9a00") },
    absorb    = hex("#FFEAC7"),
  },
}

-- Retourne un petit aperçu des couleurs du thème soigneur (utile pour l’UI)
function DFFT.GetHealThemeSwatches(name)
  local t = DFFT.HealThemes[name or ""] or DFFT.HealThemes["Mint"]
  return {
    targetSmall   = copyRGB(t.target.small),
    targetMedium  = copyRGB(t.target.medium),
    targetBig     = copyRGB(t.target.big),
    targetVBig    = copyRGB(t.target.vbig),
    ntSmall       = copyRGB(t.nontarget.small),
    ntMedium      = copyRGB(t.nontarget.medium),
    ntBig         = copyRGB(t.nontarget.big),
    ntVBig        = copyRGB(t.nontarget.vbig),
    absorb        = copyRGB(t.absorb),
  }
end

-- Applique les couleurs heals/absorbs dans la DB (plusieurs champs pour compat)
function DFFT.ApplyHealTheme(name)
  DB.healTheme = name or "Mint"
  local t = DFFT.HealThemes[DB.healTheme] or DFFT.HealThemes["Mint"]
  if not t then return end

  DB.healer = DB.healer or {}
  DB.healer.colors = DB.healer.colors or {}
  DB.healer.absorbs = DB.healer.absorbs or {}

  -- Structure moderne
  DB.healer.colors.target = {
    small  = copyRGB(t.target.small),
    medium = copyRGB(t.target.medium),
    big    = copyRGB(t.target.big),
    vbig   = copyRGB(t.target.vbig),
  }
  DB.healer.colors.nontarget = {
    small  = copyRGB(t.nontarget.small),
    medium = copyRGB(t.nontarget.medium),
    big    = copyRGB(t.nontarget.big),
    vbig   = copyRGB(t.nontarget.vbig),
  }
  DB.healer.colors.absorb = copyRGB(t.absorb)

  -- Champs pratiques / héritage (compat éventuelle avec d’autres modules)
  DB.healer.healSmallColor     = copyRGB(t.target.small)
  DB.healer.healMediumColor    = copyRGB(t.target.medium)
  DB.healer.healBigColor       = copyRGB(t.target.big)
  DB.healer.healVeryBigColor   = copyRGB(t.target.vbig)

  DB.healer.nonTargetHealSmallColor   = copyRGB(t.nontarget.small)
  DB.healer.nonTargetHealMediumColor  = copyRGB(t.nontarget.medium)
  DB.healer.nonTargetHealBigColor     = copyRGB(t.nontarget.big)
  DB.healer.nonTargetHealVeryBigColor = copyRGB(t.nontarget.vbig)

  DB.healer.absorbColor   = copyRGB(t.absorb)
  DB.healer.absorbs.color = copyRGB(t.absorb)

  -- Valeurs par défaut de visibilité d’absorbs si absentes
  if DB.healer.absorbs.show == nil then DB.healer.absorbs.show = true end
  if DB.healer.showAbsorbs == nil then DB.healer.showAbsorbs = DB.healer.absorbs.show end
  if DB.showAbsorbs == nil then DB.showAbsorbs = DB.healer.absorbs.show end
  DB.healer.absorbsEnabled = DB.healer.absorbs.show

  if print then print(("|cff88ff88SCT|r 治疗主题已应用: %s"):format(DB.healTheme)) end
end
